#!/bin/bash
BaseDir=$(cd "$(dirname "$0")"; pwd)
cd ${BaseDir}

set -x
set -o errexit

find . -name "*.sh"| xargs chmod +x

function download(){
 echo "check download package..."

 [ -f sealos ] || wget -c "https://sealyun.oss-cn-beijing.aliyuncs.com/latest/sealos"
 [ -f kube1.18.0.tar.gz ] || wget -c "https://sealyun.oss-cn-beijing.aliyuncs.com/7b6af025d4884fdd5cd51a674994359c-1.18.0/kube1.18.0.tar.
 gz"
 
 [ -f dashboard.tar ] || wget "https://github.com/sealstore/dashboard/releases/download/v2.0.0-bata5/dashboard.tar"
}

download

# 0启动2个虚拟机
vagrant up

# 1 在这2个机器上安装k8s
# https://github.com/fanux/sealos
if [ -f  sealos ]; then
 echo "sealos is ready"
else
 download
 exit 1
fi

# 下载离线资源包
if [ ! -f kube1.18.0.tar.gz ]; then
 download
 echo "downlod package"
 exit 1
fi

#确保默认路由是192.168.99.1
ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "sudo service network restart"
ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.11 "sudo service network restart"

sleep 3
# 安装kubernetes集群
ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "cd /vagrant/; sealos init --user root --pk ./private_key \
	--master 192.168.99.10 \
	--node 192.168.99.11 \
	--pkg-url /vagrant/kube1.18.0.tar.gz \
	--version v1.18.0"
:<<EOF
节点机如何访问master的apiserver ipvs https://github.com/fanux/sealos/issues/134
yum install -y ipvsadm
ipvsadm -Ln

# cat /etc/hosts
10.103.97.2 apiserver.cluster.local
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.103.97.2:6443 rr
  -> 192.168.99.10:6443           Masq    1      2          0  

EOF

# 2 k8s dashboard的部署
# dashboard https://sealyun.com/docs/app.html
if [ -f dashboard.tar ]; then
 echo "dashboard"
else
 exit 1
fi

#ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "cd /vagrant/; sealos install --pkg-url dashboard.tar"
ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "cd /vagrant/; sealos install --pkg-url dashboard.tar"
#  imagePullPolicy: Always 修改成 IfNotPresent【不然离线时，docker仓库不能连接启动不了ImagePullBackOff】
ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "cd /vagrant/;  kubectl apply -f manifests"
echo "dashboard https://192.168.99.10:32000 "
#高版本的chrome对自签名证书不能访问，简单修改启动方法即可 Exec=/usr/bin/google-chrome-stable %U --ignore-certificate-errors

#命令行取token
# echo $(kubectl get secret -nkubernetes-dashboard     $(kubectl get secret -n kubernetes-dashboard|grep dashboard-token |awk '{print $1}')     -o jsonpath='{.data.token}'  | base64 --decode)
:<<EOF
或者访问的token从安装的这里取
01:05:44 [INFO] [ssh.go:51] [192.168.99.10:22] deployment.apps/dashboard-metrics-scraper created
01:05:45 [INFO] [ssh.go:51] [192.168.99.10:22] eyJhbGciOiJ****
EOF

# 3 kubesphere hostpath卷只在worker上用[为方便调试分析机器上都建下]
# 检查集群正常后执行 install-ks.sh 安装
echo "install-ks.sh ks|alone 或者 install-ks.sh 进行离线或在线安装kubesphere"

ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "sudo cp -f /etc/kubernetes/admin.conf /vagrant/."
:<<EOF
没有配置k8s的机器上kubectl执行方法
export KUBECONFIG=/vagrant/admin.conf
kubectl get nodes
EOF

echo "运行这个获取dashboard的访问信息 ./acessDash.sh "